package com.stone.bigdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stone.bigdata.constant.TimeConstant;
import com.stone.bigdata.dao.fota.ProductListDao;
import com.stone.bigdata.domain.ProductList;
import com.stone.bigdata.domain.VO.CityTotalVO;
import com.stone.bigdata.domain.VO.UserStateVO;
import com.stone.bigdata.service.ProductListService;
import com.stone.bigdata.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProductListServiceImpl implements ProductListService {

    @Autowired
    private ProductListDao productListDao; // fota

    @Resource
    private RedisUtil redisUtil;

    @Override
    public Integer getTotalAdd(Timestamp zero, Timestamp now) {
        Integer todayAdd = productListDao.getTotalAdd(zero, now);
        return todayAdd;
    }

    @Override
    public List<CityTotalVO> listCityTotal() {
        return productListDao.listCityTotal();
    }

    @Override
    public List<CityTotalVO> listCityActiveTotal(Timestamp yesterdayZero, Timestamp yesterdayEnd) {
        return productListDao.listCityActiveTotal(yesterdayZero, yesterdayEnd);
    }
    @Override
    public List<UserStateVO> listUserState() {
        return productListDao.listUserState();
    }

    @Override
    public List<Integer> listHoursAdd() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime zero = LocalDateTime.of(now.toLocalDate(), LocalTime.MIN);
        LocalDateTime nine = zero.plusHours(9);
        LocalDateTime twelve = zero.plusHours(12);
        LocalDateTime fifteen = zero.plusHours(15);
        LocalDateTime eighteen = zero.plusHours(18);
        LocalDateTime twentyOne = zero.plusHours(21);
        LocalDateTime end = LocalDateTime.of(now.toLocalDate(), LocalTime.MAX);
        List<Integer> result = new ArrayList<>();
        if (now.isAfter(nine)) {
            if (redisUtil.hasKey(TimeConstant.NINE)) {
                result.add(Integer.parseInt(redisUtil.get(TimeConstant.NINE).toString()));
            } else {
                Timestamp begin = Timestamp.valueOf(zero);
                Timestamp tail = Timestamp.valueOf(nine);
                Integer nineAdd = productListDao.getTotalAdd(begin, tail);
                result.add(nineAdd);
                redisUtil.set(TimeConstant.NINE, nineAdd);
            }

            if (redisUtil.hasKey(TimeConstant.TWELVE)) {
                result.add(Integer.parseInt(redisUtil.get(TimeConstant.TWELVE).toString()));
            } else {
                if(now.isAfter(twelve)) {
                    Timestamp begin = Timestamp.valueOf(nine);
                    Timestamp tail = Timestamp.valueOf(twelve);
                    Integer twelveAdd = productListDao.getTotalAdd(begin, tail);
                    result.add(twelveAdd);
                    redisUtil.set(TimeConstant.TWELVE, twelveAdd);
                } else {
                    return result;
                }
            }

            if (redisUtil.hasKey(TimeConstant.FIFTEEN)) {
                result.add(Integer.parseInt(redisUtil.get(TimeConstant.FIFTEEN).toString()));
            } else {
                if(now.isAfter(fifteen)) {
                    Timestamp begin = Timestamp.valueOf(twelve);
                    Timestamp tail = Timestamp.valueOf(fifteen);
                    Integer fifteenAdd = productListDao.getTotalAdd(begin, tail);
                    result.add(fifteenAdd);
                    redisUtil.set(TimeConstant.FIFTEEN, fifteenAdd);
                } else {
                    return result;
                }
            }

            if (redisUtil.hasKey(TimeConstant.EIGHTEEN)) {
                result.add(Integer.parseInt(redisUtil.get(TimeConstant.EIGHTEEN).toString()));
            } else {
                if(now.isAfter(eighteen)) {
                    Timestamp begin = Timestamp.valueOf(fifteen);
                    Timestamp tail = Timestamp.valueOf(eighteen);
                    Integer eighteenAdd = productListDao.getTotalAdd(begin, tail);
                    result.add(eighteenAdd);
                    redisUtil.set(TimeConstant.EIGHTEEN, eighteenAdd);
                } else {
                    return result;
                }
            }

            if (redisUtil.hasKey(TimeConstant.TWENTY_ONE)) {
                result.add(Integer.parseInt(redisUtil.get(TimeConstant.TWENTY_ONE).toString()));
            } else {
                if(now.isAfter(twentyOne)) {
                    Timestamp begin = Timestamp.valueOf(eighteen);
                    Timestamp tail = Timestamp.valueOf(twentyOne);
                    Integer twentyOneAdd = productListDao.getTotalAdd(begin, tail);
                    result.add(twentyOneAdd);
                    redisUtil.set(TimeConstant.TWENTY_ONE, twentyOneAdd);
                } else {
                    return result;
                }
            }

            if(now.isAfter(twentyOne) && now.isBefore(end)) {
                Timestamp begin = Timestamp.valueOf(twentyOne);
                Timestamp tail = Timestamp.valueOf(end);
                Integer twentyOneAdd = productListDao.getTotalAdd(begin, tail);
                result.add(twentyOneAdd);
            }

            return result;
        } else {
            return null;
        }
    }

    @Override
    public Integer getTotal() {
     return productListDao.getTotal();
    }
}
